package com.example.planetexploder.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.constants.ShelfConst;
import com.example.planetexploder.constants.SortConst;

import java.util.ArrayList;

/**
 * 摘抄总表操作集
 * @author 宋熙然
 * 修改日期：2019.10.27
 * 完成测试，修复若干bug
 */
public class ShelfTable {

    private final DBHelper helper;

    public ShelfTable(Context context) {
        helper = new DBHelper(context);
        helper.getWritableDatabase();
    }

    /**
     * 向总表中添加摘抄集信息
     * @param bookInfo 摘抄集信息
     */
    public void add_BookInfo_To_ShelfTable(BookInfo bookInfo) {
        Log.d(LogConst.MTAG, "add_BookInfo_To_ShelfTable()");
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(ShelfConst.SHELF_BOOK_ID, bookInfo.bookId);
        cv.put(ShelfConst.SHELF_BOOK_NAME, bookInfo.bookName);
        cv.put(ShelfConst.SHELF_DATE_MOD, bookInfo.dateMod);
        cv.put(ShelfConst.SHELF_BOOK_TAG1, bookInfo.tag1);
        cv.put(ShelfConst.SHELF_BOOK_TAG2, bookInfo.tag2);
        cv.put(ShelfConst.SHELF_BOOK_TAG3, bookInfo.tag3);
        db.insert(ShelfConst.SHELF_NAME, null, cv);
        db.close();
    }

    /**
     * 从总表中删除摘抄集信息
     * @param bookId 摘抄集id
     */
    public void delete_BookInfo_From_ShelfTable(String bookId) {
        Log.d(LogConst.MTAG, "delete_BookInfo_From_ShelfTable()");
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "DELETE FROM " + ShelfConst.SHELF_NAME + " WHERE " + ShelfConst.SHELF_BOOK_ID + " = ?;";
        db.execSQL(sql, new Object[]{bookId});
        db.close();
    }

    /**
     * 获取摘抄集信息
     * @param bookId 摘抄集id
     * @return 摘抄集信息
     */
    public BookInfo get_BookInfo(String bookId) {
        Log.d(LogConst.MTAG, "get_BookInfo()");
        SQLiteDatabase db = helper.getReadableDatabase();
        BookInfo bookInfo = new BookInfo();
        Cursor c = db.query(ShelfConst.SHELF_NAME, null, ShelfConst.SHELF_BOOK_ID + "=?", new String[]{bookId}, null, null, null);
        if(c.moveToFirst()) {
            bookInfo.bookId = bookId;
            bookInfo.bookName = c.getString(c.getColumnIndex(ShelfConst.SHELF_BOOK_NAME));
            bookInfo.dateMod = c.getString(c.getColumnIndex(ShelfConst.SHELF_DATE_MOD));
            bookInfo.tag1 = c.getInt(c.getColumnIndex(ShelfConst.SHELF_BOOK_TAG1));
            bookInfo.tag2 = c.getInt(c.getColumnIndex(ShelfConst.SHELF_BOOK_TAG2));
            bookInfo.tag3 = c.getInt(c.getColumnIndex(ShelfConst.SHELF_BOOK_TAG3));
        }
        c.close();
        db.close();
        return bookInfo;
    }

    /**
     * 更新摘抄集信息
     * @param bookInfo 摘抄集信息
     */
    public void update_BookInfo(BookInfo bookInfo) {
        Log.d(LogConst.MTAG, "update_BookInfo()");
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "UPDATE " + ShelfConst.SHELF_NAME + " SET "
                + ShelfConst.SHELF_BOOK_NAME + "=?,"
                + ShelfConst.SHELF_DATE_MOD + "=?,"
                + ShelfConst.SHELF_BOOK_TAG1 + "=?,"
                + ShelfConst.SHELF_BOOK_TAG2 + "=?,"
                + ShelfConst.SHELF_BOOK_TAG3 + "=? "
                + "WHERE " + ShelfConst.SHELF_BOOK_ID + "=?;";
        db.execSQL(sql, new Object[]{bookInfo.bookName, bookInfo.dateMod, bookInfo.tag1, bookInfo.tag2, bookInfo.tag3, bookInfo.bookId});
        db.close();
    }

    /**
     * 获得总表中的信息
     * @param order 排序方式
     * @see SortConst 排序方式相关常量
     * @param tag1  不为0时按tag1的值检索
     * @param tag2  不为0时按tag2的值检索
     * @return 总表中的信息
     */
    public ArrayList<BookInfo> get_ShelfTable(int order, int tag1, int tag2) {
        Log.d(LogConst.MTAG, "get_ShelfTable()");
        ArrayList<BookInfo> list = new ArrayList<>();
        SQLiteDatabase db = helper.getReadableDatabase();

        String orderBy;
        if(order == SortConst.ORDER_BY_CRE_UP) { //按创建日期升序
            orderBy = ShelfConst.SHELF_BOOK_ID + " ASC";
        }
        else if(order == SortConst.ORDER_BY_CRE_DOWN) {  //按创建日期降序
            orderBy = ShelfConst.SHELF_BOOK_ID + " DESC";
        }
        else /*if(order == SortConst.ORDER_BY_MOD)*/ {  //按修改日期（降序）
            orderBy = ShelfConst.SHELF_DATE_MOD + " DESC";
        }

        //选出特定标签
        Cursor c;
        if(tag1 == 0){
            if(tag2 == 0){
                c = db.query(ShelfConst.SHELF_NAME, null, null, null, null, null, orderBy);
            }else{
                c = db.query(ShelfConst.SHELF_NAME, null, ShelfConst.SHELF_BOOK_TAG2 + "=?", new String[]{""+tag2}, null, null, orderBy);
            }
        }else{
            if(tag2 == 0){
                c = db.query(ShelfConst.SHELF_NAME, null, ShelfConst.SHELF_BOOK_TAG1+ "=?", new String[]{""+tag1}, null, null, orderBy);
            }else{
                c = db.query(ShelfConst.SHELF_NAME, null, ShelfConst.SHELF_BOOK_TAG1 + "=? AND " + ShelfConst.SHELF_BOOK_TAG2 + "=?", new String[]{""+tag1,""+tag2}, null, null, orderBy);
            }
        }

        //读取元素
        while (c.moveToNext()){
            BookInfo bookInfo = new BookInfo();
            bookInfo.bookId = c.getString(c.getColumnIndex(ShelfConst.SHELF_BOOK_ID));
            bookInfo.bookName = c.getString(c.getColumnIndex(ShelfConst.SHELF_BOOK_NAME));
            bookInfo.dateMod = c.getString(c.getColumnIndex(ShelfConst.SHELF_DATE_MOD));
            bookInfo.tag1 = c.getInt(c.getColumnIndex(ShelfConst.SHELF_BOOK_TAG1));
            bookInfo.tag2 = c.getInt(c.getColumnIndex(ShelfConst.SHELF_BOOK_TAG2));
            bookInfo.tag3 = c.getInt(c.getColumnIndex(ShelfConst.SHELF_BOOK_TAG3));
            list.add(bookInfo);
        }

        c.close();
        db.close();
        return list;
    }

}
